#include "dev_sys.h"
#include "dev_log.h"

namespace hisilicon{namespace dev{

    bool sys::init(ot_vi_video_mode video_mode)
    {
        ot_vb_cfg vb_cfg;
        td_s32 ret;

        ss_mpi_sys_exit();
        ss_mpi_vb_exit();

        memset(&vb_cfg,0,sizeof(ot_vb_cfg));
        vb_cfg.common_pool[0].blk_size = 3840 * 2160 * 2;
        vb_cfg.common_pool[0].blk_cnt = 20;
        vb_cfg.common_pool[1].blk_size = 704 * 576 * 3 / 2;
        vb_cfg.common_pool[1].blk_cnt = 10;
        vb_cfg.max_pool_cnt= 2;
        ret = ss_mpi_vb_set_cfg(&vb_cfg);
        if(ret != TD_SUCCESS)
        {
            DEV_WRITE_LOG_ERROR("ss_mpi_vb_set_cfg failed with error 0x%x",ret);
            return false;
        }

        ot_vb_supplement_cfg vb_supplement_cfg = {0};
        vb_supplement_cfg.supplement_cfg = OT_VB_SUPPLEMENT_BNR_MOT_MASK;
        ret = ss_mpi_vb_set_supplement_cfg(&vb_supplement_cfg);
        if (ret != TD_SUCCESS)
        {
            DEV_WRITE_LOG_ERROR("ss_mpi_vb_set_supplement failed with error 0x%x",ret);
            return false;
        }

        ret = ss_mpi_vb_init();
        if (ret != TD_SUCCESS)
        {
            DEV_WRITE_LOG_ERROR("ss_mpi_vb_init failed with error 0x%x",ret);
            return false;
        }

        ret = ss_mpi_sys_init();
        if (ret != TD_SUCCESS)
        {
            DEV_WRITE_LOG_ERROR("ss_mpi_sys_init failed with error 0x%x",ret);
            return false;
        }

        ot_vi_vpss_mode vi_vpss_mode;
        memset(&vi_vpss_mode,0,sizeof(vi_vpss_mode));
        for(int i = 0; i < OT_VI_MAX_PIPE_NUM; i++)
        {
            vi_vpss_mode.mode[i] = OT_VI_OFFLINE_VPSS_OFFLINE;
        }
        ret = ss_mpi_sys_set_vi_vpss_mode(&vi_vpss_mode);
        if(ret != TD_SUCCESS)
        {
            DEV_WRITE_LOG_ERROR("ss_mpi_sys_set_vi_vpss_mode failed with error 0x%x",ret);
            return false;
        }

        ret = ss_mpi_sys_set_vi_video_mode(video_mode);
        if(ret != TD_SUCCESS)
        {
            DEV_WRITE_LOG_ERROR("ss_mpi_sys_set_vi_video_mode failed with error 0x%x",ret);
            return false;
        }

        return true;
    }

    void sys::release()
    {
        ss_mpi_sys_exit();
        ss_mpi_vb_exit_mod_common_pool(OT_VB_UID_VDEC);
        ss_mpi_vb_exit();
    }

    ot_vb_pool sys::create_pool(td_u64 blk_size,td_u32 blk_cnt,ot_vb_remap_mode mode)
    {
        ot_vb_pool_cfg vb_pool_cfg = {0};

        vb_pool_cfg.blk_size = blk_size;
        vb_pool_cfg.blk_cnt = blk_cnt;
        vb_pool_cfg.remap_mode = mode;
        return ss_mpi_vb_create_pool(&vb_pool_cfg);
    }

    void sys::destroy_pool(ot_vb_pool pool_id)
    {
        ss_mpi_vb_destroy_pool(pool_id);
    }

}}//namespace
